<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js对象之属性特征</title>
</head>
<body>
  <ul>
    <li>可通过Object.getOwnPropertyDescriptor(s)查看对象的(所有)属性特征(值，可修改，可遍历，是否可删除或重复配置)</li>
    <li>可通过Object.defineProperty()新增属性，配置属性特征(是否可修改，可遍历，是否可删除或重复配置)</li>
    <li>可通过Object.defineProperties()批量配置对象的多个属性特征</li>
  </ul>
  <script>
    'use strict'
    const user = { name: 'Miracle', age: 35 }
    console.log(Object.getOwnPropertyDescriptor(user, 'name'))    // {value: "Miracle", writable: true, enumerable: true, configurable: true}
    console.log(JSON.stringify(Object.getOwnPropertyDescriptors(user), null, 2))
    // {
    //   "name": {
    //     "value": "Miracle",
    //     "writable": true,
    //     "enumerable": true,
    //     "configurable": true
    //   },
    //   "age": {
    //     "value": 35,
    //     "writable": true,
    //     "enumerable": true,
    //     "configurable": true
    //   }
    // }
    // 为user对象新增web属性
    Object.defineProperty(user, 'web', {
      value: 'https://github.com/miracle-git'
    })
    console.log(user)                                             // {name: "Miracle", age: 35, web: "https://github.com/miracle-git"}
    // 配置user属性不可修改(默认true)
    Object.defineProperty(user, 'web', {
      writable: false
    })
    // user.web = 'https://github.com/miracle-git/fts'            // 严格模式下报错，非严格模式忽略
    // 配置user属性不可遍历(默认true)
    Object.defineProperty(user, 'web', {
      enumerable: false
    })
    console.log(Object.keys(user))                                // ["name", "age"]
    for (const key in user) {
      console.log(key)                                            // user,age
    }
    // 配置user属性不可配置(默认true)
    Object.defineProperty(user, 'web', {
      configurable: false
    })
    // delete user.web                                            // 严格模式下报错，非严格模式忽略
    // Object.defineProperty(user, 'web', {
    //   configurable: true
    // })
    Object.defineProperties(user, {
      name: {
        writable: false,
        enumerable: false,
        configurable: true
      },
      age: {
        writable: false,
        enumerable: false,
        configurable: true
      }
    })
    console.log(Object.keys(user))                                // []
  </script>
</body>
</html>
